package tt.dz.appserver.service.business.imp;

import java.util.List;

import javax.annotation.Resource;

import org.apache.avro.AvroRemoteException;

import com.alibaba.fastjson.JSONObject;

import tt.dz.appserver.dao.ext.ServiceStationDao;
import tt.dz.appserver.entity.model.ServiceStation;
import tt.dz.appserver.service.business.ServiceStationService;
import tt.dz.appserver.util.date.DateUtil;
import tt.dz.appserver.util.enums.Response;

/**
 * 服务网点管理实现类
 * @author USER001
 *
 */

public class ServiceStationServiceImpl implements ServiceStationService {

	@Resource
	ServiceStationDao serviceStationDao;
	
	@Override
	public JSONObject servicestationadd(String servicestationname, String description,
			String businesstime, String contactphone, String provinceid, String cityid, String areaid,
			String outletsaddr, String longitude, String latitude, String operid)
			throws AvroRemoteException {
		// TODO Auto-generated method stub
		ServiceStation serviceStation = new ServiceStation();
		JSONObject resultJsonObject = new JSONObject();
		String createupdatetime = DateUtil.getDateTime();
		Integer validflag = 1;		
		
		String servicestationid = DateUtil.getDateTimeLong().substring(2, 14);
		
		serviceStation.setService_station_id(servicestationid);
		serviceStation.setService_station_name(servicestationname);
		serviceStation.setDescription(description);
		serviceStation.setBusiness_time(businesstime);
		serviceStation.setContact_phone(contactphone);
		serviceStation.setProvince_id(provinceid);
		serviceStation.setCity_id(cityid);
		serviceStation.setArea_id(areaid);
		serviceStation.setOutlets_addr(outletsaddr);
		serviceStation.setLongitude(Float.parseFloat(longitude));
		serviceStation.setLatitude(Float.parseFloat(latitude));
		serviceStation.setCreate_time(DateUtil.parseStringToDateTime(createupdatetime));
		serviceStation.setUpdate_time(DateUtil.parseStringToDateTime(createupdatetime));
		serviceStation.setOper_id(operid);
		serviceStation.setValid_flag(validflag);
		
		serviceStationDao.addServiceStation(serviceStation);
		
		resultJsonObject.put("servicestationid", servicestationid);
		resultJsonObject.put("servicestationname", servicestationname);
		resultJsonObject.put("description", description);
		resultJsonObject.put("businesstime", businesstime);
		resultJsonObject.put("contactphone", contactphone);
		resultJsonObject.put("provinceid", provinceid);
		resultJsonObject.put("cityid", cityid);
		resultJsonObject.put("areaid", areaid);
		resultJsonObject.put("outletsaddr", outletsaddr);
		resultJsonObject.put("longitude", longitude);
		resultJsonObject.put("latitude", latitude);
		resultJsonObject.put("createtime", createupdatetime);
		resultJsonObject.put("updatetime", createupdatetime);
		resultJsonObject.put("operid", operid);
		resultJsonObject.put("validflag", validflag);
		resultJsonObject.put("code", Response.SUCCESS.getCode());
		resultJsonObject.put("msg", Response.SUCCESS.getMsg());
		
		return resultJsonObject;
	}

	@Override
	public JSONObject servicestationdel(String servicestationid, String operid)
			throws AvroRemoteException {
		// TODO Auto-generated method stub
		ServiceStation serviceStation = new ServiceStation();
		JSONObject resultJsonObject = new JSONObject();
		String updatetime = DateUtil.getDateTime();
		Integer validflag = 0;		
		
		serviceStation.setService_station_id(servicestationid);
		serviceStation.setUpdate_time(DateUtil.parseStringToDateTime(updatetime));
		serviceStation.setOper_id(operid);
		serviceStation.setValid_flag(validflag);
		
		serviceStationDao.delServiceStation(serviceStation);
		
		resultJsonObject.put("servicestationid", servicestationid);
		resultJsonObject.put("updatetime", updatetime);
		resultJsonObject.put("operid", operid);
		resultJsonObject.put("validflag", validflag);
		resultJsonObject.put("code", Response.SUCCESS.getCode());
		resultJsonObject.put("msg", Response.SUCCESS.getMsg());
		
		return resultJsonObject;
	}

	@Override
	public JSONObject servicestationupdate(String servicestationid, String servicestationname, String description,
			String businesstime, String contactphone, String provinceid, String cityid, String areaid,
			String outletsaddr, String longitude, String latitude, String operid)
			throws AvroRemoteException {
		// TODO Auto-generated method stub
		ServiceStation serviceStation = new ServiceStation();
		JSONObject resultJsonObject = new JSONObject();
		String updatetime = DateUtil.getDateTime();
		Integer validflag = 1;
		
		serviceStation.setService_station_id(servicestationid);
		serviceStation.setService_station_name(servicestationname);
		serviceStation.setDescription(description);
		serviceStation.setBusiness_time(businesstime);
		serviceStation.setContact_phone(contactphone);
		serviceStation.setProvince_id(provinceid);
		serviceStation.setCity_id(cityid);
		serviceStation.setArea_id(areaid);
		serviceStation.setOutlets_addr(outletsaddr);
		serviceStation.setLongitude(Float.parseFloat(longitude));
		serviceStation.setLatitude(Float.parseFloat(latitude));
		serviceStation.setUpdate_time(DateUtil.parseStringToDateTime(updatetime));
		serviceStation.setOper_id(operid);
		serviceStation.setValid_flag(validflag);
		
		serviceStationDao.updateServiceStation(serviceStation);
		
		resultJsonObject.put("servicestationid", servicestationid);
		resultJsonObject.put("servicestationname", servicestationname);
		resultJsonObject.put("description", description);
		resultJsonObject.put("businesstime", businesstime);
		resultJsonObject.put("contactphone", contactphone);
		resultJsonObject.put("provinceid", provinceid);
		resultJsonObject.put("cityid", cityid);
		resultJsonObject.put("areaid", areaid);
		resultJsonObject.put("outletsaddr", outletsaddr);
		resultJsonObject.put("longitude", longitude);
		resultJsonObject.put("latitude", latitude);
		resultJsonObject.put("updatetime", updatetime);
		resultJsonObject.put("operid", operid);
		resultJsonObject.put("validflag", validflag);
		resultJsonObject.put("code", Response.SUCCESS.getCode());
		resultJsonObject.put("msg", Response.SUCCESS.getMsg());
		
		return resultJsonObject;
	}

	@Override
	public List<ServiceStation> servicestationselect(String servicestationname, String provinceid, 
			String cityid, String areaid) throws AvroRemoteException {
		// TODO Auto-generated method stub
		
		return serviceStationDao.findServiceStationValid(servicestationname, provinceid, cityid, areaid);
		
	}

}
